<?php

namespace libs;

class database {
    
    public static $_config;
    private static $_instance;
    private static $_connection;
    private static $_query;
    
    public function __construct() 
    {
    }
    
    public static function getInstance()
    {
        if(empty(self::$_instance))
        {
            self::$_instance = new self();
        }
        
        return self::$_instance;
    }
    
    public static function connect() 
    {
        if(empty(self::$_connection))
        {
            $config = self::$_config;
            
            $connection = new database\Connector($config);
            
            $connection = $connection::choose();

            self::$_connection = $connection;
        }
        
        return self::$_connection;
    }
    
    public static function query()
    {
        if(empty(self::$_query))
        {
            try{

              if(empty(self::$_config->driver))
              {
                  throw new \Exception('Wybierz sterownik ( plik config.ini, opcja "database.driver" ).');
              }
              
                $driver = strtolower(self::$_config->driver);

                $db = 'libs\database\\'.$driver.'\\query';

                self::$_query = new $db(self::connect());
            }
            
            catch(\Exception $e)
            {
                echo $e->getMessage();
                die();
            }
        }
        
        return self::$_query;
    }
   
}

?>
